/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Jzdj;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.JzdjService;
import me.zhengjie.business.service.dto.JzdjDto;
import me.zhengjie.business.service.dto.JzdjQueryCriteria;
import me.zhengjie.config.DebugProperties;
import me.zhengjie.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-06-06
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "就诊登记管理")
@RequestMapping("/api/jzdj")
public class JzdjController {

    private final JzdjService jzdjService;
    private final GsglService gsglService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('jzdj:list')")
    public void exportJzdj(HttpServletResponse response, JzdjQueryCriteria criteria) throws IOException {
        jzdjService.download(jzdjService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询就诊登记")
    @PreAuthorize("@el.check('jzdj:list')")
    public ResponseEntity<Object> queryJzdj(JzdjQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(jzdjService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
     * 查询在院人员，即已经就诊登记的人员
     * @param criteria
     * @param pageable
     * @return
     */
    @GetMapping(value = "/queryZyry")
    @ApiOperation("查询在院人员")
    @PreAuthorize("@el.check('jzdj:list')")
    public ResponseEntity<Object> queryZyry(JzdjQueryCriteria criteria, Pageable pageable){
        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        return new ResponseEntity<>(jzdjService.queryZyry(gsgl.getBisGsglYljgdm(), criteria.getBlurry(), pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/findJzjl")
    @ApiOperation("根据身份证查询入院的人员")
    public ResponseEntity<Object> findJzjl(@RequestParam String sfz) {
        return new ResponseEntity<>(jzdjService.findJzjl(sfz),HttpStatus.OK);
    }

    @GetMapping(value = "/queryLocalFyxx")
    @ApiOperation("查询本地费用信息")
    public ResponseEntity<Object> queryLocalFyxx(@RequestParam Long jzid) {
        return new ResponseEntity<>(jzdjService.queryLocalFyxx(jzid),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增就诊登记")
    @ApiOperation("新增就诊登记")
    public ResponseEntity<Object> createJzdj(@Validated @RequestBody Jzdj resources){
        log.info("ryrq: {} ", resources.getBisJzdjRyrq());
        JzdjDto jzdjDto = jzdjService.create(resources);
        if(!DebugProperties.debug) {
            log.info("生产模式");
            jzdjService.makeJzdj84(jzdjDto.getBisJzdjId(), "", "1");
        } else {
            log.info("开发模式");
        }
        return new ResponseEntity<>(jzdjDto, HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改就诊登记")
    @ApiOperation("修改就诊登记")
    public ResponseEntity<Object> updateJzdj(@Validated @RequestBody Jzdj resources){
        jzdjService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除就诊登记")
    @ApiOperation("删除就诊登记")
    @PreAuthorize("@el.check('jzdj:del')")
    public ResponseEntity<Object> deleteJzdj(@RequestBody Long[] ids) {
        jzdjService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping(value = "/exeWfcy")
    @Log("机构执行无费退院")
    @ApiOperation("机构执行无费退院")
    public ResponseEntity<Object> exeWfcy(@RequestParam Long jzid, @RequestParam String cyrq) {
        if(!DebugProperties.debug) {
            log.info("生产模式");
            jzdjService.makeJzdj84(jzid, cyrq, "-1");
        } else {
            Jzdj jzdj = new Jzdj();
            jzdj.setBisJzdjId(jzid);
            jzdj.setBisJzdjCyrq(cyrq);
            jzdj.setBisJzdjCyzt("1");
            jzdj.setBisJzdjJylx("-1");
            jzdjService.update(jzdj);
            log.info("开发模式");
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping(value = "/exeScAllFymx")
    @Log("机构执行上传所有费用明细")
    @ApiOperation("机构执行上传所有费用明细")
    public ResponseEntity<Object> exeScAllFymx(@RequestParam Long jzid) {
        String msg = "";
        if(!DebugProperties.debug) {
            log.info("生产模式");
            msg = jzdjService.makeFymx84(jzid);
        } else {
            log.info("开发模式");
        }
        return new ResponseEntity<>(msg, HttpStatus.CREATED);
    }

    @PostMapping(value = "/exeDlAllFymx")
    @Log("机构执行删除所有费用明细")
    @ApiOperation("机构执行删除所有费用明细")
    public ResponseEntity<Object> exeDlAllFymx(@RequestParam Long jzid) {
        String msg = "";
        if(!DebugProperties.debug) {
            log.info("生产模式");
            msg = jzdjService.deleteFymx(jzid);
        } else {
            log.info("开发模式");
        }
        return new ResponseEntity<>(msg, HttpStatus.CREATED);
    }

    @PostMapping(value = "/exeScSingleFymx")
    @Log("机构执行单独费用上报")
    @ApiOperation("机构执行单独费用上报")
    public ResponseEntity<Object> exeScSingleFymx(@RequestParam Long fyid, @RequestParam String ywbm) {
        String msg = "";
        if(!DebugProperties.debug) {
            log.info("生产模式");
            msg = jzdjService.makeFymx84Single(fyid, ywbm);
        } else {
            log.info("开发模式");
        }
        return new ResponseEntity<>(msg, HttpStatus.CREATED);
    }

    @PostMapping(value = "/exeYjs")
    @Log("机构执行费用预结算")
    @ApiOperation("机构执行费用预结算")
    public ResponseEntity<Object> exeYjs(@RequestParam Long jzid, @RequestParam String cyrq) {
        String msg = "";
        if(!DebugProperties.debug) {
            log.info("生产模式");
            jzdjService.makeJzdj84(jzid, cyrq, "2");
            msg = jzdjService.makeFyjs84(jzid, "1");
        } else {
            log.info("开发模式");
        }
        return new ResponseEntity<>(msg, HttpStatus.CREATED);
    }

    @PostMapping(value = "/exeFyjs")
    @Log("机构执行费用结算")
    @ApiOperation("机构执行费用结算")
    public ResponseEntity<Object> exeFyjs(@RequestParam Long jzid, @RequestParam String cyrq) {
        String msg = "";
        if(!DebugProperties.debug) {
            log.info("生产模式");
            jzdjService.makeJzdj84(jzid, cyrq, "2");
            msg = jzdjService.makeFyjs84(jzid, "0");
            // jzdjService.saveFyjs84(msg, cyrq, jzid);
        } else {
            log.info("开发模式");
        }
        return new ResponseEntity<>(msg, HttpStatus.CREATED);
    }

    @PostMapping(value = "/exeQftf")
    @Log("机构执行清方退费")
    @ApiOperation("机构执行清方退费")
    public ResponseEntity<Object> exeQftf(@RequestParam Long jzid, @RequestParam String jsxxId) {
        String msg = "";
        if(!DebugProperties.debug) {
            log.info("生产模式");
            msg = jzdjService.makeFyjs84(jzid, "-3");
            // jzdjService.saveQftf84(jsxxId);
        } else {
            log.info("开发模式");
        }
        return new ResponseEntity<>(msg, HttpStatus.CREATED);
    }

    @GetMapping(value = "/queryPersonFee")
    @ApiOperation("查询人员是否缴费")
    public ResponseEntity<Object> queryPersonFee(@RequestParam String ryxx, @RequestParam String djrq, @RequestParam String cblx) {
        return new ResponseEntity<>(jzdjService.queryPersonFee(ryxx, djrq, cblx), HttpStatus.OK);
    }

    @ApiOperation("查询批量过方的在院人员")
    @GetMapping(value = "/queryJzdjResultVo")
    public ResponseEntity<Object> queryJzdjResultVo(JzdjQueryCriteria criteria) {
        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        return new ResponseEntity<>(jzdjService.queryJzdjResultVo(criteria.getKfrq(), gsgl.getBisGsglYljgdm(), criteria.getKsmc(), criteria.getSndj(), criteria.getRyxx()), HttpStatus.OK);
    }

    @PostMapping(value = "/exePlgf")
    @Log("批量过方")
    @ApiOperation("批量过方")
    public ResponseEntity<Object> exePlgf(@RequestParam("ids[]") List<Long> ids, @RequestParam String ryxx,
                                          @RequestParam String sndj, @RequestParam String kfrq, @RequestParam String masterId) {
        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        String idstring = StringUtils.join(ids, ",").equals("0") ? "" : StringUtils.join(ids, ",");
        log.info("ids:{}", idstring);
        jzdjService.makeFymxPl(idstring, ryxx, sndj, kfrq, gsgl.getBisGsglYljgdm(), masterId, userid);
        jzdjService.makeYjsPl(idstring, ryxx, sndj, gsgl.getBisGsglYljgdm(), userid);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
}